//
//  DetailViewController.m
//  知乎日报
//
//  Created by 陈明光 on 2019/9/6.
//  Copyright © 2019 陈明光. All rights reserved.
//

#import "DetailViewController.h"
#import "NewsDetailModel.h"
#import "StroriesModel.h"
@interface DetailViewController ()<UIWebViewDelegate,UIScrollViewDelegate>
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)NewsDetailModel *model;
@end

@implementation DetailViewController
-(UIWebView *)webView{
    if (!_webView) {
        _webView=[[UIWebView alloc]init];
        _webView.delegate=self;
        _webView.scrollView.delegate=self;
        _webView.backgroundColor=[UIColor whiteColor];
        
        _webView.frame=CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT-20);
    }
    return _webView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.webView];
    
    [self getNewsDetailForID:self.stroriesModel.id];
}
//获取详细新闻
-(void)getNewsDetailForID:(NSInteger)ID{
    //iOS9 以后使用https
    [[AFHTTPSessionManager manager]GET:[NSString stringWithFormat:@"https://news-at.zhihu.com/api/4/news/%ld",ID] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [NewsDetailModel mj_setupObjectClassInArray:^NSDictionary *{
            return @{
              @"section":@"SectionModel"
              };
        }];
        NewsDetailModel *model=[NewsDetailModel mj_objectWithKeyValues:responseObject];
        model.HTML=[NSString stringWithFormat:@"<html><head><link rel=\"stylesheet\" href=%@></head><body>%@</body></html>",model.css[0],model.body];
        self.model=model;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
-(void)setModel:(NewsDetailModel *)model{
    _model=model;
    
    [self.webView loadHTMLString:model.HTML baseURL:nil];
}

@end
